/**
 * \file xdmxcon/mainwindow.c
 * \author Christian Spoo
 * \date 08.08.2010
 *
 * XDMXcon main window
 */

#include <glib-object.h>

#include "../config.h"
#include "mainwindow.h"

enum {
  NAME_COLUMN,
  ACTIVE_COLUMN,
  FADE_TIME_COLUMN,
  WAIT_TIME_COLUMN,
  FADE_DIR_COLUMN,

  N_COLUMNS
};

GtkWidget *mainwindow_new(void) {
  /* Create main window */
  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title(GTK_WINDOW(window), "XDMXcon");
  gtk_window_maximize(GTK_WINDOW(window));

  /* Delete the window upon close event */
  g_signal_connect(window, "delete-event", G_CALLBACK(mainwindow_delete_event), NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(mainwindow_destroy_callback), NULL);
  
  mainwindow_initialize_menu(window);
  mainwindow_initialize_gui(window);

  return window;
}

void mainwindow_initialize_menu(GtkWidget *window) {
  GtkWidget *menubar = gtk_menu_bar_new();

  GtkWidget *file_menu = gtk_menu_new();
  GtkWidget *file_item = gtk_menu_item_new_with_mnemonic("_File");
  GtkWidget *file_exit_item = gtk_menu_item_new_with_mnemonic("E_xit");

  GtkWidget *help_menu = gtk_menu_new();
  GtkWidget *help_item = gtk_menu_item_new_with_mnemonic("_Help");
  GtkWidget *help_about_item = gtk_menu_item_new_with_mnemonic("_About...");

  /* The menus are the menubar's items' children */
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(help_item), help_menu);
  gtk_menu_shell_append(GTK_MENU_SHELL(help_menu), help_about_item);

  gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
  gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_exit_item);

  /* Append the menubar items to the menubar */
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file_item);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help_item);

  g_object_set_data(G_OBJECT(window), "menubar", menubar);

  g_signal_connect_swapped(file_exit_item, "activate", G_CALLBACK(mainwindow_destroy_callback), (gpointer)"file.exit");
  g_signal_connect_swapped(help_about_item, "activate", G_CALLBACK(mainwindow_help_about_callback), NULL);
}

void mainwindow_initialize_gui(GtkWidget *window) {
  /* Box layouts */
  GtkWidget *hbox, *vbox;

  /* Status bar */
  GtkWidget *statusbar;
  gchar *str;

  /* Menu bar */
  GtkWidget *menubar;
  
  /* Chaser list view */
  GtkListStore *active_chaser_store;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkWidget *chaserlist;
  GtkWidget *chaserlist_label;
 
  /* Set up chaser list view */
  active_chaser_store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_UINT);
  chaserlist = gtk_tree_view_new_with_model(GTK_TREE_MODEL(active_chaser_store));
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("Chaser", renderer, "text", NAME_COLUMN, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(chaserlist), column);
  g_object_set(chaserlist, "width-request", 300, NULL);

  /* Set up status bar */
  statusbar = gtk_statusbar_new();
  str = g_strdup_printf("XDMXcon ready");
  g_object_set_data(G_OBJECT(window), "statusbar", statusbar);
  gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), str), str); 

  /* Horizontal alignment of chaser list and central control panel */
  hbox = gtk_hbox_new(FALSE, 0);
  vbox = gtk_vbox_new(FALSE, 0);

  chaserlist_label = gtk_label_new("Active chasers:");
  gtk_label_set_justify(GTK_LABEL(chaserlist_label), GTK_JUSTIFY_LEFT);
  g_object_set(chaserlist_label, "width-request", 300, NULL);

  gtk_box_pack_start(GTK_BOX(vbox), chaserlist_label, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), chaserlist, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);

  /* Vertical alignment of menubar, central hbox and statusbar */
  vbox = gtk_vbox_new(FALSE, 0);
  menubar = (GtkWidget *)g_object_get_data(G_OBJECT(window), "menubar");
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
  gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
  gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);

  /* Add vbox to window */
  gtk_container_add(GTK_CONTAINER(window), vbox);

  /* Show all widgets */
  gtk_widget_show_all(window);
}

gboolean mainwindow_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
  return FALSE;
}

void mainwindow_destroy_callback(GtkWidget *widget, gpointer data) {
  gtk_main_quit();
}

void mainwindow_help_about_callback(GtkWidget *widget, gpointer data) {
  GtkWidget *dialog = gtk_about_dialog_new();
  gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "XDMXcon");
  gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), VERSION);
  gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), "(c) Christian Spoo");
  gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog), "http://dmxcon.googlecode.com/");  

  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog);
}

